package com.powerhua.core.base;

import com.powerhua.core.bean.BeansRegisterLocator;
import com.powerhua.core.utils.CommonUtils;
import com.powerhua.core.web.*;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
 * @author Brian.Xie
 */
public class BaseManagerServletAction implements IServletAction {

    private static final Log LOG = LogFactory.getLog(BaseManagerServletAction.class);
    
    @Override
    public String doAction(HttpServletRequest request) {
        String result = "";
        Map map = request.getParameterMap();
        String action = request.getParameter("a");

        BaseManager baseManager = (BaseManager) BeansRegisterLocator.getBean("coreBaseManager");
        String baseModelId = request.getParameter("i");
        if ("save".equals(action)) {
            result = baseManager.save(map, baseModelId);
        } else if ("delete".equals(action)) {
            result = baseManager.delete(map, baseModelId);
        } else if ("list".equals(action)) {
            String pageSize = request.getParameter("pageSize");
            String pageNum = request.getParameter("pageNum");
            int intPageSize = CommonUtils.converStr2Int(pageSize);
            int intPageNum = CommonUtils.converStr2Int(pageNum);
            if (intPageSize == 0) {
                intPageSize = 30;
            }
            if (intPageNum == 0) {
                intPageNum = 1;
            }
            result = baseManager.list(map, baseModelId, intPageSize, intPageNum);
        } else if ("update".equals(action)) {
            LOG.info("update action begin");
            result = baseManager.update(map, baseModelId);
            LOG.info("update action end");
        } else if ("getObjectById".equals(action)) {
            result = baseManager.getObjectById(map, baseModelId);
        } else {
            // TODO 输出错误编码与内容
            result = "error";
        }

        //
        return result;
    }
}
